package psn.kiko.service.business.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import psn.kiko.constant.DBConstant;
import psn.kiko.constant.MessageConstant;
import psn.kiko.excepion.BaseBusinessException;
import psn.kiko.mapper.business.system.RoleMapper;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.dto.InterfaceDTO;
import psn.kiko.pojo.dto.RoleDTO;
import psn.kiko.pojo.entity.system.RoleEntity;
import psn.kiko.pojo.vo.DelDetailVO;
import psn.kiko.pojo.vo.InterfaceVO;
import psn.kiko.pojo.vo.LabelValue;
import psn.kiko.pojo.vo.RoleVo;
import psn.kiko.service.business.associated.InterfaceRoleService;
import psn.kiko.service.business.associated.UserRoleService;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色管理服务
 */
@Service
public class RoleService{
	@Autowired
	RoleMapper roleMapper;
	@Autowired
	InterfaceRoleService interfaceRoleService;
	@Autowired
	UserRoleService userRoleService;
	
	/**
	 * 分页查询角色、携带查询条件
	 */
	public PageResult pagingWithCondition(RoleDTO roleDTO,int pageNum,int pageSize){
		Page<Object> page = PageHelper.startPage(pageNum,pageSize);
		RoleEntity roleEntity = new RoleEntity();
		BeanUtils.copyProperties(roleDTO,roleEntity);
		List<RoleVo> roleVoList = roleMapper.selectList(roleEntity);
		PageResult<Object> pageResult = PageResult.builder()
				.total((int) page.getTotal())
				.data(roleVoList)
				.build();
		return pageResult;
	}
	
	/**
	 * 添加角色
	 */
	public Integer addRole(RoleDTO roleDTO){
		try{
			RoleEntity roleEntity = new RoleEntity();
			BeanUtils.copyProperties(roleDTO,roleEntity);
			ArrayList<RoleEntity> list = new ArrayList<>();
			list.add(roleEntity);
			return roleMapper.insertBatch(list);
		}catch(Exception e){
			e.printStackTrace();
			if(e instanceof DuplicateKeyException){
				throw new BaseBusinessException(MessageConstant.INSERT_DUPLICATE_ROWS);
			}else
				throw new BaseBusinessException(MessageConstant.UNKNOWN_ERROR);
		}
	}
	
	/**
	 * 获取角色状态选项
	 */
	public List<Short> getEnables(){
		return DBConstant.getStatuses();
	}
	
	/**
	 * 根据id批量删除角色
	 * <p style="color:red;">添加事务</p>
	 */
	@Transactional
	public List<DelDetailVO> batchDeleteByRoleIds(List<Integer> roleIds){
		//删除 用户-角色
		Integer c1 = userRoleService.batchDeleteByRoleIds(roleIds);
		//删除 接口-角色
		Integer c2 = interfaceRoleService.batchDeleteByRoleIds(roleIds);
		//批量删除角色表本身数据
		Integer c3 = roleMapper.deleteBatchByIds(roleIds);
		
		ArrayList<DelDetailVO> delDetailVOS = new ArrayList<>();
		
		delDetailVOS.add(new DelDetailVO("删除的角色数量",c3));
		delDetailVOS.add(new DelDetailVO("与这批角色解除关联的用户数量",c1));
		delDetailVOS.add(new DelDetailVO("与这批角色解除关联的API数量",c2));
		
		return delDetailVOS;
	}
	
	/**
	 * 更新角色
	 */
	public Integer updateRole(RoleDTO roleDTO){
		RoleEntity roleEntity = new RoleEntity();
		BeanUtils.copyProperties(roleDTO,roleEntity);
		return roleMapper.updateRole(roleEntity);
	}
	
	/**
	 * 根据主键批量启用角色
	 */
	public Integer enableRoleByIds(List<Integer> roleIds){
		return roleMapper.updateStatusByIds(roleIds,DBConstant.ENABLE);
	}
	
	/**
	 * 根据主键批量禁用角色
	 */
	public Integer disableRoleByIds(List<Integer> roleIds){
		return roleMapper.updateStatusByIds(roleIds,DBConstant.DISABLE);
	}
	
	/**
	 * 批量插入角色
	 */
	public Integer addRoles(List<RoleDTO> roleDTOs){
		ArrayList<RoleEntity> roleEntities = new ArrayList<>();
		for(RoleDTO roleDTO: roleDTOs){
			RoleEntity roleEntity = new RoleEntity();
			BeanUtils.copyProperties(roleDTO,roleEntity);
			roleEntities.add(roleEntity);
		}
		return roleMapper.insertBatch(roleEntities);
	}
	
	/**
	 * 根据主键获取角色
	 */
	public RoleEntity getRoleById(Integer roleId){
		return roleMapper.selectById(roleId);
	}
	
	/**
	 * 给角色添加一批接口
	 */
	public Integer batchAddApisToRole(int roleId,List<Integer> apiStableIds){
		return interfaceRoleService.allocateMultiInterfacesToOneRole(roleId,apiStableIds);
	}
	
	/**
	 * 分页获取角色已分配的接口，携带查询条件
	 */
	public PageResult getAllocatedApisOfRole(int pageNum,int pageSize,int roleId,InterfaceDTO interfaceDTO){
		Page<Object> page = PageHelper.startPage(pageNum,pageSize);
		List<InterfaceVO> list = interfaceRoleService.getAllocatedApisByRoleId(roleId,interfaceDTO);
		PageResult<Object> pageResult = PageResult.builder()
				.total(((int) page.getTotal()))
				.data(list)
				.build();
		return pageResult;
	}
	
	/**
	 * 分页获取角色未分配到的接口、携带查询条件
	 */
	public PageResult getUnallocatedApisOfRole(int pageNum,int pageSize,int roleId,InterfaceDTO interfaceDTO){
		Page<Object> page = PageHelper.startPage(pageNum,pageSize);
		List<InterfaceVO> list = interfaceRoleService.getUnallocatedApisByRoleId(roleId,interfaceDTO);
		PageResult<Object> pageResult = PageResult.builder()
				.total(((int) page.getTotal()))
				.data(list)
				.build();
		return pageResult;
	}
	
	/**
	 * 移除角色的一批接口（删除中间表数据）
	 */
	public Integer batchDeleteApisOfRole(int roleId,List<Integer> apiStableIds){
		return interfaceRoleService.batchDeleteByRoleIdAndApiIds(roleId,apiStableIds);
	}
	
	/**
	 * 禁用角色的一批接口
	 */
	public Integer batchDisableApisOfRole(int roleId,List<Integer> apiStableIds){
		return interfaceRoleService.batchDisableByRoleId(roleId,apiStableIds);
	}
	
	/**
	 * 启用角色的一批接口
	 */
	public Integer batchEnableApisOfRole(int roleId,List<Integer> apiStableIds){
		return interfaceRoleService.batchEnableByRoleId(roleId,apiStableIds);
	}
	
	/**
	 * 获取角色状态选项
	 */
	public List<LabelValue> getStatusesWithLabel(){
		return DBConstant.getStatusesWithLabel();
	}
	
}
